import { post } from 'utils/request';
import { saveToken, saveUser, autoRedirect } from 'utils';

$('#myForm').validator().on('submit', function (e) {
  if (e.isDefaultPrevented()) {
    console.log('表单验证不通过');
  } else {
    e.preventDefault();
    let post_data = {
      username: this.username.value,
      password: this.password.value,
    };
    let $btn = $(this).find('.btn').button('loading');
    post('user/tokens', post_data).done(d => {
      let remember = this.remember.checked;
      saveToken(d.token, remember);
      saveUser(d.user, remember);
      this.reset();
      alert('登录成功');
      autoRedirect();
    }).always(()=>{
      $btn.button('reset');
    });
  }
});
